﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


using ClassBLL;
using ClassModel;
using ClassCommon;
using ClassWeb.App_Data;

namespace ClassWeb.Controllers
{
    public class TestController : BaseController
    {


        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult InsertTest(string userAccount, string userPassword)
        {
            this.Response.ContentType = "application/json";
            t_classDemo_UserInfo userInfo = new t_classDemo_UserInfo();
            userInfo.userAccount = userAccount;
            userInfo.userPassword = userPassword;
            userInfo.createTime = DateTime.Now;

            int increaseID = t_classDemo_UserInfo_BLL.InsertAccount(userInfo);

            // 注册成功，记录cookie
            HttpCookie userLoginCookie = new HttpCookie("userID", increaseID.ToString());
            this.Response.Cookies.Add(userLoginCookie);

            return Json(new { status = true, accountID = increaseID }, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 测试验证码
        /// </summary>
        /// <returns></returns>
        public ActionResult TestValidateCode()
        {
            //首先实例化验证码的类  
            ValidateCodeHelper validateCode = new ValidateCodeHelper();
            //生成验证码指定的长度  
            string code = validateCode.GetRandomString(4);
            //创建验证码的图片  
            byte[] bytes = validateCode.CreateImage(code);

            // 存储cookie
            HttpCookieHelper.SetCookie("validateCookie", code);

            //最后将验证码返回  
            return File(bytes, @"image/jpeg");
        }

        [HttpGet]
        public ActionResult upload()
        {
            return View();
        }

        [HttpPost]
        public ActionResult upload(string placeHoderParam)
        {
            this.Response.ContentType = "application/json";

            UploadFileInfo uploadInfo = HttpPostFileHelper.FileUploadAndSave();

            t_class_fileupload fileuploadModel = new t_class_fileupload();
            fileuploadModel.userId = HttpCookieHelper.GetUserID();
            fileuploadModel.orginName = uploadInfo.orginName;
            fileuploadModel.newName = uploadInfo.newName;
            fileuploadModel.fileSize = uploadInfo.fileSize;
            fileuploadModel.extensionName = uploadInfo.extensionName;
            fileuploadModel.savePath = uploadInfo.savePath;
            fileuploadModel.contentType = uploadInfo.contentType;
            fileuploadModel.uploadDate = DateTime.Now;

            int increaseID = t_class_user_fileupalod_BLL.InsertFileUpload(fileuploadModel);

            return Json(new { uploadInfo = uploadInfo, increaseID = increaseID }, JsonRequestBehavior.AllowGet);
        }


        [HttpGet]
        public ActionResult getDataTxt(string taskid)
        {
            this.Response.ContentType = "application/json";

            int decodeTaskId = Convert.ToInt32(EncryptHelper.Decrypt(taskid));

            string dataTxt = t_class_CreateTxt_BLL.createDataTxt(decodeTaskId, this.CurrentUser.id);

            return Json(new { txt = dataTxt }, JsonRequestBehavior.AllowGet);

        }

    }
}
